package org.jiamandan.com.config;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.stereotype.Component;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-10-21 15:22
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-10-21 15:22	         Jmd        	创建
 * @end
 */
@Component
public class NettyInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
//        pipeline.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN,1024,1,2,2,0,true));
//        pipeline.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN,1024,3,2,0,0,true));

//        pipeline.addLast(new NettyDecoder(ByteOrder.LITTLE_ENDIAN,1024,1,3,2,1,2,0,0,true));
        pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
//        pipeline.addLast(new NettyDecoder());
        // 添加对byte数组的编解码，netty提供了很多编解码器，你们可以根据需要选择
        //已分隔符区分整包消息 $_
//        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
//        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));

        pipeline.addLast(new ByteArrayDecoder());
        pipeline.addLast(new ByteArrayEncoder());
        // 添加上自己的处理器
//        pipeline.addLast(new NettyHandler());
        pipeline.addLast(new TcpInboundAdapter());
    }
}
